package com.lilang.a0608provider;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class ListAdapter  extends BaseAdapter {
    List<Book> books;
    Context context;

    public ListAdapter(List<Book> books, Context context) {
        this.books = books;
        this.context = context;
    }

    @Override
    public int getCount() {
        return books!=null?books.size():0;
    }

    @Override
    public Object getItem(int position) {
        return books.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHodler hodler=null;
        if (convertView==null){
            hodler=new ViewHodler();
            convertView= LayoutInflater.from(context).inflate(R.layout.listviewlayout,null);
            hodler.bookname=convertView.findViewById(R.id.bookname);
            hodler.author=convertView.findViewById(R.id.author);
            convertView.setTag(hodler);
        }else{
            hodler= (ViewHodler) convertView.getTag();
        }
        hodler.bookname.setText(books.get(position).getBookname());
        hodler.author.setText(books.get(position).getAuthor());
        return convertView;
    }

    public class ViewHodler{
        TextView bookname,author;
    }
}
